<?php

namespace backend\controllers;

use Yii;
use backend\models\Project;
use backend\models\ProjectDonation;
use backend\models\ProjectDonationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use Da\User\Filter\AccessRuleFilter;
use yii\web\UploadedFile;

/**
 * ProjectDonationController implements the CRUD actions for ProjectDonation model.
 */
class ProjectDonationController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'ruleConfig' => [
                    'class' => AccessRuleFilter::class,
                ],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create'],
                        'roles' => ['createProject'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['update'],
                        'roles' => ['updateProject'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['delete'],
                        'roles' => ['deleteProject'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


    /**
     * Creates a new ProjectDonation model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($project_id)
    {
        $projectData = Project::find()->where(['id' => $project_id])->asArray()->one();

        $model = new ProjectDonation();
        $model->project_id = $projectData['id'];
        $model->registry_number = ProjectDonation::generateRegistryNumber();
        $model->is_direct_donation = ProjectDonation::CONTRACT;
        $model->is_proxy = ProjectDonation::OWN;

        if ($model->load(Yii::$app->request->post())) 
        {
            $model->contractFile = UploadedFile::getInstance($model, 'contractFile');

            if($model->validate())
            {
                if($model->save(false))
                {
                    return $this->redirect(['project/view-donation-contract', 'id' => $projectData['id']]);
                }
            }
        }

        return $this->render('create', [
            'projectData' => $projectData,
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing ProjectDonation model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id, $project_id)
    {
        $model = $this->findModel($id);
        $projectData = Project::find()->where(['id' => $project_id])->asArray()->one();

        if ($model->load(Yii::$app->request->post())) 
        {
            $model->contractFile = UploadedFile::getInstance($model, 'contractFile');

            if($model->validate())
            {
                if($model->save(false))
                {
                    return $this->redirect(['project/view-donation-contract', 'id' => $projectData['id']]);
                }
            }
        }

        return $this->render('update', [
            'projectData' => $projectData,
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing ProjectDonation model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id, $project_id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['project/view-donation-contract', 'id' => $project_id]);
    }

    /**
     * Finds the ProjectDonation model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ProjectDonation the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ProjectDonation::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
